package gui;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

public class ProgressRenderer extends DefaultTableCellRenderer {
	
    private final JProgressBar b = new JProgressBar(0, 100);
    private final JPanel p = new JPanel(new BorderLayout());
    
    public ProgressRenderer() {
        super();
        
        setOpaque(true);
        p.add(b);
        p.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
    }
    
    @Override 
    public Component getTableCellRendererComponent(JTable table, 
    		Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Integer i = Integer.parseInt((String)value);
        
        String text = "Done";
      
        if(i==-1) {
            text = "";
        }else if (i==-2){
        	text="Canceled";
        }else if(i<100) {
            b.setValue(i);
            return p;
        }
        
        super.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, column);
        
        return this;
    }
    @Override public void updateUI() {
        super.updateUI();
        
        if( p != null ) 
        	SwingUtilities.updateComponentTreeUI(p);
    }
}
